using System;
using System.Linq;
using GiveGraphModel;

namespace GiveGraphMappers
{
    public class UserMapper : AbstractGiveGraphMapper<User>
    {
        #region Overrides of AbstractGiveGraphMapper<User>

        protected override int GetElementKey(User t)
        {
            return t.MemberId;
        }

        #endregion

        public User GetByEmail(string email)
        {
            return Call<User>((ctx) => (DoGetByEmail(ctx, email)));
        }

        protected User DoGetByEmail(GiveGraphContext ctx, string email)
        {
            return (from user in ctx.Users
                   where user.FacebookToken.Equals(email) || user.GoogleUserId.Equals(email)
                   select user).SingleOrDefault();
        }

        public bool UserExists(string email)
        {
            return Call<bool>((ctx) => (DoUserExists(ctx, email)));
        }

        protected bool DoUserExists(GiveGraphContext ctx, string email)
        {
            return (from user in ctx.Users
                    where user.FacebookToken.Equals(email) || user.GoogleUserId.Equals(email)
                    select user).SingleOrDefault() != null;
        }
    }
}